package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.dto.CollectionDto;
import com.heima.article.entity.ApCollection;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-19
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {


    //文章收藏
    @Override
    public ResponseResult<ApCollection> collectionArticle(CollectionDto dto) {

        ApCollection apCollection = new ApCollection();
        apCollection.setArticleId(dto.getArticleId());
        apCollection.setEntryId(14);
        apCollection.setCollectionTime(new Date());

        LambdaQueryWrapper<ApCollection> query=new LambdaQueryWrapper<>();
        query.eq(ApCollection::getArticleId,dto.getArticleId());
        if (dto.getOperation()==0) {
            this.save(apCollection);
        }else {
            this.remove(query);
        }

        return ResponseResult.okResult();
    }

    /**
     * 查询文章收藏
     * @param articleId
     * @return
     */
    @Override
    public ApCollection getApCollection(Long articleId) {

        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApCollection::getArticleId,articleId);
        ApCollection one = this.getOne(wrapper);
        return one;
    }
}
